package com.fxp.project.jiuselu.web.business.v1.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.domain.CommonResult;
import com.fxp.project.jiuselu.web.models.coupon.service.IJslUserCouponService;
import com.fxp.project.jiuselu.web.models.goods.service.IJslGoodsAttrService;
import com.fxp.project.jiuselu.web.models.goods.vo.GoodsAttrVo;
import com.fxp.project.jiuselu.web.models.order.entity.JslOrder;
import com.fxp.project.jiuselu.web.models.order.entity.JslOrderGoods;
import com.fxp.project.jiuselu.web.models.order.entity.JslOrderRetreatGoods;
import com.fxp.project.jiuselu.web.models.order.service.IJslOrderGoodsService;
import com.fxp.project.jiuselu.web.models.order.service.IJslOrderRetreatGoodsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.html.Option;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@RequestMapping("/v1/order/retreat")
@Log4j2
@RestController
@Transactional
public class OrderRetreatGoodsController extends BaseController {
    @Autowired
    IJslOrderRetreatGoodsService iJslOrderRetreatGoodsService;
    @Autowired
    IJslUserCouponService jslUserCouponService;
    @Autowired
    IJslOrderGoodsService jslOrderGoodsService;
    @Autowired
    IJslGoodsAttrService jslGoodsAttrService;

    /**
     * 新增订单退货
     */
    @PostMapping("save")
    @CrossOrigin
    public CommonResult Save(@RequestBody @Validated JslOrderRetreatGoods jslOrderRetreatGoods, BindingResult result) {
        super.checkBindResult(result);
        if (!Optional.ofNullable(iJslOrderRetreatGoodsService.getOne(new QueryWrapper<JslOrderRetreatGoods>().eq("order_id",jslOrderRetreatGoods.getOrderId()))).isPresent()) {
            jslOrderRetreatGoods.setUserId(super.getCurrentUserInfo().getUserId());
            jslOrderRetreatGoods.setCreateBy(super.getCurrentUserInfo().getNickName());
            jslOrderRetreatGoods.setCreateTime(LocalDateTime.now());
            iJslOrderRetreatGoodsService.save(jslOrderRetreatGoods);
            jslUserCouponService.updateCouponStatus(jslOrderRetreatGoods.getOrderId());
            //退货返还库存数量\
            if(jslOrderRetreatGoods.getType()==0){
            List<JslOrderGoods> jslOrderGoods = jslOrderGoodsService.selectOrderGoodsByOrderId(jslOrderRetreatGoods.getOrderId());
            for(JslOrderGoods jsl : jslOrderGoods){
                Integer goodsNum = jsl.getGoodsNum();
                GoodsAttrVo goodsAttrVo = jslGoodsAttrService.selectGodosAttrVoListBtAttrId(jsl.getGoodsAttrId());
                Integer storeCount = goodsAttrVo.getStoreCount();
                Integer stock=goodsNum + storeCount;
                jslGoodsAttrService.updateStoreCountByGoodsIdAndAttrId(stock, jsl.getGoodsId() ,jsl.getGoodsAttrId());
                }
            }
            return CommonResult.success(jslOrderRetreatGoods);
        }else {
            return CommonResult.failed("退货已申请，请勿重复提交");
        }
    }

    /**
     * 上传退货物流信息
     */
    @PostMapping("edit")
    @CrossOrigin
    public CommonResult edit(@RequestBody HashMap map, BindingResult result) {
        super.checkBindResult(result);
        JslOrderRetreatGoods order_id = iJslOrderRetreatGoodsService.getOne(new QueryWrapper<JslOrderRetreatGoods>().eq("order_id", map.get("orderId")));
        order_id.setLogisticCode(map.get("logisticName").toString());
        order_id.setLogisticName(map.get("logisticCode").toString());
        order_id.setUpdateBy(super.getCurrentUserInfo().getNickName());
        order_id.setUpdateTime(LocalDateTime.now());
        order_id.setState(3L);
        iJslOrderRetreatGoodsService.updateById(order_id);
        return CommonResult.success(order_id);
    }


    /**
     * 查看退换货详情
     * @param jslOrderRetreatGoods
     * @param result
     * @return
     */
    @PostMapping("retreatDetails")
    @CrossOrigin
    public CommonResult retreatDetails(@RequestBody @Validated JslOrderRetreatGoods jslOrderRetreatGoods, BindingResult result) {
        super.checkBindResult(result);
        JslOrderRetreatGoods data = iJslOrderRetreatGoodsService.getOne(new QueryWrapper<JslOrderRetreatGoods>().eq("order_id", jslOrderRetreatGoods.getOrderId()));
        if(data==null){
            return CommonResult.failed("未查到退换货详情！");
        }
        return CommonResult.success(data.getGoodsName());
    }
}

